#include <iostream>
#include <vector>
#include <queue>
#include <memory>
#include "Mutex.hpp"
#include "Thread.hpp"
#include "Logger.hpp"
#include "Cond.hpp"
#pragma once

namespace ThreadPoolMudule
{
    using namespace LockModule;
    using namespace ThreadModule;
    using namespace LogMudule;
    using namespace CondModule;

    // 线程指针
    using thread_t = std::shared_ptr<Thread>;

    const int defaultnum = 5;
    template <typename T>
    class ThreadPool
    {
    private:
        bool IsEmpty() { return _taskq.empty(); }
        void HanderTask(std::string name)
        {
            // 一旦线程创建就持续委派任务
            while (true)
            {
                T t;
                // 进入任务队列
                {
                    LockGuard lc(_lock);
                    while (IsEmpty() && _isrunning)
                    {
                        _wait_num++;
                        _cond.Wait(_lock);
                        _wait_num--;
                    }

                    // 退出逻辑
                    if (IsEmpty() && !_isrunning)
                        break;

                    t = _taskq.front();
                    _taskq.pop();
                }
                // 执行任务
                t();
            }

            LOG(LogLevel::INFO) << "线程 " << name << " 暂停";
        }
        ThreadPool(int num = defaultnum)
            : _num(num), _wait_num(0), _isrunning(false)
        {
            for (int i = 0; i < _num; i++)
            {
                _threads.push_back(std::make_shared<Thread>(std::bind(&ThreadPool::HanderTask, this, std::placeholders::_1)));
                //LOG(LogLevel::INFO) << "构建线程 " << _threads.back()->Name() << " 成功";
            }
        }

    public:
        static ThreadPool<T> *getInstance()
        {
            if (instance == NULL)
            {
                LockGuard lockguard(mutex);
                if (instance == NULL)
                {
                    LOG(LogLevel::INFO) << "单例首次被执行，需要加载对象...";
                    instance = new ThreadPool<T>();
                    instance->Start();
                }
            }

            return instance;
        }
        void Start()
        {
            if (_isrunning)
                return;
            _isrunning = true;
            for (auto &ptr : _threads)
            {
              //  LOG(LogLevel::INFO) << "启动线程" << ptr->Name() << "成功";
                ptr->Start();
            }
        }
        void Equeue(T &in)
        {
            LockGuard lockguard(_lock);
            if (!_isrunning)
                return;
            _taskq.push(std::move(in));
            if (_wait_num > 0)
                _cond.Notify();
        }

        void Stop()
        {
            // 让所有线程忙完当前进程后停下来
            LockGuard lockguard(_lock);
            if (_isrunning)
            {
                _isrunning = false;
                if (_wait_num > 0)
                {
                    _cond.NotifyAll();
                }
            }
        }

        void Wait()
        {
            for (auto &ptr : _threads)
            {
                ptr->Join();
                LOG(LogLevel::INFO) << "回收线程 " << ptr->Name() << " 成功";
            }
        }
        ~ThreadPool()
        {
        }

    private:
        std::vector<thread_t> _threads; // 线程管理
        std::queue<T> _taskq;           // 临界资源
        int _num;                       // 线程数
        int _wait_num;                  // 等待队列中的任务数
        bool _isrunning;                // 标志位是否运行

        Mutex _lock;
        Cond _cond;

        static ThreadPool<T> *instance;
        static Mutex mutex; // 只用来保护单例
    };
    template <typename T>
    ThreadPool<T> *ThreadPool<T>::instance = NULL;
    template <typename T>
    Mutex ThreadPool<T>::mutex; // 只用来保护单例

} // namespace ThreadPoolMudule
